// ejeje.js
// (C) Håvard Gulldahl <ejeje@lurtgjort.no>, GPLv2
// Parts (C) Douglas Crockford, Public Domain


/*
  Remedial JavaScript
  Douglas Crockford
  http://javascript.crockford.com/remedial.html
*/

String.prototype.quote = function () {
    var c, i, l = this.length, o = '"';
    for (i = 0; i < l; i += 1) {
        c = this.charAt(i);
        if (c >= ' ') {
            if (c === '\\' || c === '"') {
                o += '\\';
            }
            o += c;
        } else {
            switch (c) {
            case '\b':
                o += '\\b';
                break;
            case '\f':
                o += '\\f';
                break;
            case '\n':
                o += '\\n';
                break;
            case '\r':
                o += '\\r';
                break;
            case '\t':
                o += '\\t';
                break;
            default:
                c = c.charCodeAt();
                o += '\\u00' + Math.floor(c / 16).toString(16) +
                    (c % 16).toString(16);
            }
        }
    }
    return o + '"';
};

String.prototype.supplant = function (o) {
    return this.replace(/{([^{}]*)}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
};

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}; 

//http://andrew.hedges.name/experiments/javascript_optimization/in_array.html
Array.prototype.has = function(o) {
  var i = this.length;
  if (i > 0) {
	 do {
		if (this[i] === o) {
		   return true;
		}
	 } while (i--);
  }
  return false;
}

var ejeje = {

    ERROR: 0,
    INFO: 1,
    DEBUG: 2,

    construct_query : function(obj) {
        s = "";
        for (z in obj) { 
            if(typeof(obj[z]) == "function") continue;
            s+=z+"="+obj[z]+"&"; 
        }
        return s;
    },
   
    bte: function(o1, o2) {
        return o1 >= o2;
    },
    lte: function(o1, o2) {
        return o2 <= o2;
    },
    bt: function(o1, o2) {
        return o2 > o2;
    },
    lt: function(o1, o2) {
        return o1 < o2;
    },

    isodate2us: function(sDate) {
        d = sDate.split("-");
        return d[1]+"/"+d[2]+"/"+d[0];
    },

    iso8601date: function(sDate) {
      try {
        spl = sDate.split("T");
        dte = spl[0].split("-");
        tme = spl[1].split(":");
        return new Date(dte[0], dte[1]-1, dte[2], tme[0], tme[1], tme[2]);
      } catch (e) {
        ejeje.log(e);
        return new Date();
      }
    },
  
    lag_li: function(liste) { 
      // Lager LI-elementer av array-verdier
      return '\n<li>'+liste.join('\n<li>');
    }, 

    lag_a: function(uri, s, tooltip, onclick) {
      if(tooltip == undefined) tooltip='';
      return '<a href="'+uri+'" onclick="'+onclick+'" title="'+tooltip+'">'+s+'</a>\n';
    },

    login: function(form, func) {
      // Logger inn basert på
      var callback =
      {
        success: function(o) { 
          var json = o.responseText.parseJSON();
          if(func != undefined)
            func(json.login == true, json);
        },
        failure: function(o) { alert('fail: '+o.responseText); },
        argument: []
      }
      var user = form.elements.namedItem('n').value;
      var pass = form.elements.namedItem('p').value;
      var transaction = YAHOO.util.Connect.asyncRequest('POST', '/_innlogging', 
          callback, 'u='+user+'&p='+pass)
    }, 
  
    jsonRequest: function(func, url, params) {
      var callback =
      {
        success: function(o) { 
          var json = o.responseText.parseJSON();
          func(json); // callback
        },
        failure: function(o) { alert('fail: '+o.responseText); },
        argument: []
      }
      if(typeof(params) != "string")
        params = this.construct_query(params);
      var transaction = YAHOO.util.Connect.asyncRequest('POST', url, callback, params);
      
    },


    log: function() {
      try {
        console.log.apply(this, arguments);
      } catch(e) {}
    },

    statusLevels: new Array('error', 'info', 'debug'),

    status: function(msg, level) {
      try {
        var el = YAHOO.util.Dom.get("statusBar");
        YAHOO.util.Dom.removeClass(el, "hidden");
        if([this.ERROR, this.INFO, this.DEBUG].has(level)) {
          var i=this.statusLevels.length;
          do {
            YAHOO.util.Dom.removeClass("statusBar", this.statusLevels[i]);
          } while (i--);
          YAHOO.util.Dom.addClass(el, this.statusLevels[level]);
        }
        el.innerHTML = msg;
      } catch(e) {
        window.status = msg;
      }
      var t = window.setTimeout(this.statusClear, 7000);
    },

    statusClear: function() {
      try {
        YAHOO.util.Dom.addClass("statusBar", "hidden");
        var i=this.statusLevels.length;
        do {
          YAHOO.util.Dom.removeClass("statusBar", this.statusLevels[i]);
        } while (i--);
        window.status='';
      } catch(e) {}
    }
        
    
      

};
